package org.xtwy.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;

public class SelectorServerSocketChannelDemo {
    public static void startServer() throws Exception {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress(8999));
        serverSocketChannel.configureBlocking(false);

        Selector selector = Selector.open();
        //将channel注册到select上
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        while (true) {
            //是否有可用的连接
            if (selector.select() > 0) {
                //遍历可用的连接
                for (SelectionKey key : selector.selectedKeys()) {
                    if (key.isAcceptable()) {
                        SocketChannel socketChannel = ((ServerSocketChannel) key.channel()).accept();
                        ByteBuffer buf = ByteBuffer.allocate(40);
                        int size = socketChannel.read(buf);
                        while (size > 0) {
                            buf.flip();
                            Charset charset = Charset.forName("UTF-8");
                            System.out.print(charset.newDecoder().decode(buf).toString());
                            size = socketChannel.read(buf);
                        }
                        buf.clear();
                        ByteBuffer response = ByteBuffer.wrap("您好!我已经收到了您的请求!".getBytes("UTF-8"));
                        socketChannel.write(response);
                        socketChannel.close();
                        selector.selectedKeys().remove(key);
                    }
                }
            }
        }
    }


    public static void main(String[] args) throws Exception {
        startServer();
    }
}
